//
//  MessageFrame.m
//  CarTown
//
//  Created by xionghao on 15/8/15.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "MessageFrame.h"
#import "MessageModel.h"
#define kFont [UIFont systemFontOfSize:12]

@implementation MessageFrame
-(void)setMessage:(MessageModel *)message{
    _message=message;
   
    _timef=CGRectMake(0, 0, XHScreenwidth, 16);
    
    if (message.receType==MessageReceiveType) {
        _iconf=CGRectMake(16, 16, 48, 48);
       
    }else{
        _iconf=CGRectMake(XHScreenwidth-8-48, 16, 48, 48);
    }
    
    CGFloat namex;
    CGFloat namey=CGRectGetMaxY(_iconf);
    CGFloat namewidth=16+CGRectGetWidth(_iconf);
    CGFloat nameheight;
    
    CGSize namesize=CGSizeMake(namewidth, CGFLOAT_MAX);
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    
    NSDictionary *attributes = @{NSFontAttributeName:kFont, NSParagraphStyleAttributeName:paragraphStyle.copy};
     CGSize nametextsize=[_message.username boundingRectWithSize:namesize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    nameheight=ceil(nametextsize.height);
    
    if (message.receType==MessageReceiveType) {
        namex=8;
    }else{
        namex=XHScreenwidth-8-namewidth;
    }
    _namef=CGRectMake(namex, namey, namewidth, nameheight);
    
    CGFloat contentx;
    CGFloat contenty=CGRectGetMaxY(_timef);
    CGFloat contentw;
    CGFloat contenth;
    
    CGSize maxszie=CGSizeMake(XHScreenwidth-32-16-2*48, MAXFLOAT);
    NSMutableParagraphStyle *contentgraphStyle = [[NSMutableParagraphStyle alloc]init];
    contentgraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    NSDictionary *contentattributes = @{NSFontAttributeName:kFont, NSParagraphStyleAttributeName:contentgraphStyle.copy};
    CGSize contenttext=[_message.detail boundingRectWithSize:maxszie options:NSStringDrawingUsesLineFragmentOrigin  attributes:contentattributes context:nil].size;
    contentw=ceil(contenttext.width);
    contenth=ceil(contenttext.height)+32;
    if (message.receType==MessageReceiveType) {
        contentx=CGRectGetMaxX(_namef)+8;
    }else{
        contentx=XHScreenwidth-contentw-16-namewidth;
    }
    _contentf=CGRectMake(contentx, contenty, contentw, contenth);
    _rowheight=MAX(CGRectGetMaxY(_contentf), CGRectGetMaxY(_namef))+16;
}
@end
